package liumengbaio.lolzhangyoubao.Activity.Activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.squareup.okhttp.Call;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import com.squareup.picasso.Picasso;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import de.hdodenhof.circleimageview.CircleImageView;
import liumengbaio.lolzhangyoubao.Activity.InFo.FriendInfo;
import liumengbaio.lolzhangyoubao.R;

public class FiendInfoActivity extends AppCompatActivity implements View.OnClickListener {
    private Button head_left_title, head_right_title, stars, btn_city;
    private CircleImageView head_icon;
    private TextView head_title, zhangyoubao_ID, user_ID, Area, signature, tv_fight_pwoer, tv_city;
    private ImageView user_image;
    private FriendInfo info;
    private JSONObject object;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fiend_info);
        iniView();
        getDataFromWeb();
    }

    private void iniView() {
        head_left_title = (Button) findViewById(R.id.head_left_title);
        head_right_title = (Button) findViewById(R.id.head_right_title);
        head_icon = (CircleImageView) findViewById(R.id.head_icon);
        head_title = (TextView) findViewById(R.id.head_title);
        tv_fight_pwoer = (TextView) findViewById(R.id.tv_fight_pwoer);
        zhangyoubao_ID = (TextView) findViewById(R.id.zhangyoubao_ID);
        stars = (Button) findViewById(R.id.stars);
        tv_city = (TextView) findViewById(R.id.tv_city);
        user_ID = (TextView) findViewById(R.id.user_ID);
        Area = (TextView) findViewById(R.id.Area);
        signature = (TextView) findViewById(R.id.signature);
        user_image = (ImageView) findViewById(R.id.user_image);
        head_left_title.setOnClickListener(this);
        head_right_title.setOnClickListener(this);

    }

    private void getDataFromWeb() {
        final String url = getIntent().getExtras().getString("url");
        new Thread(new Runnable() {
            @Override
            public void run() {
                OkHttpClient client = new OkHttpClient();
                Request request = new Request.Builder()
                        .url(url)
                        .build();
                Call call = client.newCall(request);
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(Request request, IOException e) {
                        Toast.makeText(FiendInfoActivity.this, "数据错误", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onResponse(Response response) throws IOException {
                        String result = response.body().string();
                        try {
                            object = new JSONObject(result);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        Gson gson = new Gson();
                        info = gson.fromJson(result, FriendInfo.class);
                        handler.sendEmptyMessage(1);
                    }
                });
            }
        }).start();
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1) {
                Picasso.with(FiendInfoActivity.this).load(info.getData().getAvatar()).into(head_icon);
                head_right_title.setText("关注TA");
                head_title.setText(info.getData().getNickname());
                zhangyoubao_ID.setText(info.getData().getUser_lol_info().getUser_id());
                user_ID.setText(info.getData().getUser_lol_info().getSummoner());
                Area.setText(info.getData().getUser_lol_info().getArea());
                Picasso.with(FiendInfoActivity.this).load(info.getData().getUser_lol_info().getIcon()).into(user_image);
                tv_fight_pwoer.setText(info.getData().getUser_lol_info().getZdl());
                if (object.has("constellation")) {
                    stars.setText(info.getData().getAge());
                }
                if (info.getData().getCity() != null) {
                    tv_city.setText(info.getData().getCity());
                } else {
                    tv_city.setText("未知");
                }
            }
        }
    };

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.head_left_title:
                finish();
                break;
        }
    }
}
